package com.zhiche.lisa.oms.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zhiche.lisa.core.utils.HttpClientUtil;
import com.zhiche.lisa.oms.dao.mapper.InterfaceCompensationMapper;
import com.zhiche.lisa.oms.dao.model.InterfaceCompensation;
import com.zhiche.lisa.oms.dto.PriceIncomingDTO;
import com.zhiche.lisa.oms.enums.FailureDataType;
import com.zhiche.lisa.oms.service.InterfaceCompensationService;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * oms tms 推送结算系统失败数据记录/成功需物理删除 服务实现类
 * </p>
 *
 * @author hongbo
 * @since 2018-10-10
 */
@Service
public class InterfaceCompensationServiceImpl extends ServiceImpl<InterfaceCompensationMapper, InterfaceCompensation> implements InterfaceCompensationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Value("${lisa.bms.insertPriceIncomingUrl}")
    private String insertPriceIncomingUrl;

    //连接超时时间
    @Value("${erp.socketTimeout}")
    private int socketTimeout;

    /**
     * 添加bms失败数据
     */
    public void interfaceCompensationData(String jsonData, String errorDetail, String type , String authorization){
        InterfaceCompensation interfaceCompensation=new InterfaceCompensation();
        interfaceCompensation.setDataContent(jsonData);
        interfaceCompensation.setErrorDetail(errorDetail);
        interfaceCompensation.setTryCount(0);
        interfaceCompensation.setType(type);
        this.insert(interfaceCompensation);
        List<NameValuePair> headNamePairs = Lists.newArrayList();
        headNamePairs.add(new BasicNameValuePair("Authorization", authorization));
        new  Thread(()->{
            int tryCount=3;
            int count=0;
            Integer code=0;
            do{
                count++;
                LOGGER.info("重试连接BMS 生成收入应付单据url:{},param:{},type:{}", insertPriceIncomingUrl, jsonData,type);
                String result = HttpClientUtil.postJson(insertPriceIncomingUrl, headNamePairs, jsonData, socketTimeout);
                LOGGER.info("重试连接BMS生成单据返回结果---->{}", result);
                if (!StringUtils.isEmpty(result)) {
                    JSONObject parseObject = JSONObject.parseObject(result);
                    code = parseObject.getInteger("code");
                    if (code == 0) {
                        this.deleteById(interfaceCompensation.getId());
                    } else {
                        interfaceCompensation.setTryCount(count);
                        interfaceCompensation.setErrorDetail(result);
                        this.updateById(interfaceCompensation);
                    }
                }
            }while (code!=0&&count<tryCount);
        }).start();

    }
}
